<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/16
 * Time: 17:37
 */

namespace app\admin\controller;


use think\Db;

class Yue extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $create_start_time = input('create_start_time');
            $create_end_time = input('create_end_time');
            $user_keyword = input('user_keyword');
            $yue_title = input('yue_title');
            $yue_status = input('yue_status');
            
            $where = [];
            // 用户ID|昵称
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $user_keyword . '%'];
            }
            // 约稿标题
            if(isset($yue_title) && $yue_title != ''){
                $where['yue_title'] = ['like', '%' . $yue_title . '%'];
            }
            // 状态
            if(isset($yue_status) && $yue_status != ''){
                $where['yue_status'] = $yue_status;
            }
            // 创建时间
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time != ''){
                $where['yue_time'] = ['between', [$create_start_time, $create_end_time]];
            }
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time == ''){
                $where['yue_time'] = ['>', $create_start_time];
            }
            if(isset($create_start_time) && $create_start_time == '' && isset($create_end_time) && $create_end_time != ''){
                $where['yue_time'] = ['<', $create_end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('yue')
                ->join('up_u_user', 'user_id = yue_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('yue')
                ->join('up_u_user', 'user_id = yue_user_id', 'LEFT')
                ->field('yue_id, yue_title, yue_status, yue_start_time, yue_end_time, yue_day_num, 
                yue_price, yue_time, yue_stat, user_id, user_username')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('yue_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 约稿详情
     * @return mixed
     */
    public function yueDetail(){
        $yue_id = input('yue_id');
        $yue = Db::table('up_yue')
            ->join('up_u_user', 'user_id = yue_user_id', 'LEFT')
            ->where('yue_id', $yue_id)
            ->field('yue_title, user_username, yue_status, yue_stat, yue_start_time, yue_start_time, 
            yue_day_num, yue_price, yue_explain, yue_images, yue_time')
            ->find();
        if(!empty($yue['yue_images'])){
            $images = trim($yue['yue_images'], ',');
            $yue['yue_images'] = explode(',', $images);
        }else{
            $yue['yue_images'] = [];
        }
        $this->assign('yue', $yue);
        return $this->fetch('yue/yuedetail');
    }
    
    /**
     * 应征画师
     * @return mixed|\think\response\Json
     */
    public function yuePainter(){
        if(request()->isAjax ()){
            $user_keyword = input('user_keyword');
            $yju_status = input('yju_status');
        
            $where['yju_yue_id'] = session('yue_id');
            // 用户ID|昵称
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $user_keyword . '%'];
            }
            // 状态
            if(isset($yju_status) && $yju_status != ''){
                $where['yju_status'] = $yju_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('yue_join_user')
                ->join('up_u_user', 'user_id = yju_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('yue_join_user')
                ->join('up_u_user', 'user_id = yju_user_id', 'LEFT')
                ->field('yju_id, yju_status, user_id, user_username, yju_time, yju_commit_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('yju_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $yue_id = input('yue_id');
        if(isset($yue_id) && $yue_id != ''){
            session('yue_id', $yue_id);
        }
        return $this->fetch('yue/yuepainter');
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function yueDown(){
        $yue_id = input('id');
        $res = Db::table('up_yue')->where('yue_id', $yue_id)->setField('yue_stat', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function yueOn(){
        $yue_id = input('id');
        $res = Db::table('up_yue')->where('yue_id', $yue_id)->setField('yue_stat', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}